import {handleCopy} from "./index.js";
import {getUrlApi} from "../api/business/sales";
import {getRecycleUrlApi, getPhoneUploadImgApi} from "../api/business/recycle";
import {generateMainImageApi} from "../api/business/staylnventory";
import {downloadTemplateApi} from "../api/business/automaticShelves";


export default {
  // 库存 预估售价历史记录处理 \n2024年07月09日 11:57:24/957\n2024年07月09日/957
  splitRecord(str) {
    if (!str) return [];
    let records = str.split('\n');

    let list = [];
    if (records && records.length > 0) {
      records.map((i, indx) => {
        if (i && indx <= 9) {
          const [date, price] = i.split('/');
          list.unshift({
            date, price
          })
        }
      })
    }
    return list;
  },

  // 动态拖动图片
  onDragEnd(event, images) {
    const {newIndex, oldIndex} = event;
    const newPic = images[newIndex];
    const oldPic = images[oldIndex];
    images[oldIndex] = oldPic;
    images[newIndex] = newPic;
  },


  // 获取链接 2 回收 1-销售
  getUrlWithId(form, type) {
    if (type != 2) {
      getUrlApi({
        id: form.id
      }).then(res => {
        if (res.code = 200)
          handleCopy(res.msg);
      })
    } else {
      getRecycleUrlApi({
        id: form.id
      }).then(res => {
        if (res.code = 200)
          handleCopy(res.msg);
      })
    }
  },

  // 生成主图
  generateMainImage(id) {
    generateMainImageApi().then(res => {
    })
  },

  // 日期格式化
  formatDate(date) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hours = date.getHours();
    let minutes = date.getMinutes();
    let seconds = date.getSeconds();

    // 格式化每个部分，确保始终是两位数字
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    hours = hours < 10 ? '0' + hours : hours;
    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds;

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },

  // 不同菜单 在手机上传的图片 1 库存 2 销售 3 回收
  getPhoneUploadImgByType(id, type, field, fn) {
    getPhoneUploadImgApi(id, type).then(res => {
      if (res.data.length > 0) {
        res.data.map(i => {
          const url = field.find(item => item == i);
          if (url == undefined) {
            field.push(i);
          }
        })
        fn && fn();
      }
    })
  },

  // 账号监控 下载模板
  handleDownloadTemplate() {
    downloadTemplateApi().then(res => {
      console.log(res.msg)
      const a = document.createElement('a');
      a.setAttribute('href', res.msg);
      a.setAttribute('target', '_blank');
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    })
  },
}
